using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsPHIEU_IN_NHAN
    {
        public clsPHIEU_IN_NHAN(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        #region Private methods
        private THONG_TIN_NHAN GetDataRow(DataRow row)
        {
            THONG_TIN_NHAN objTHONG_TIN_NHAN = new THONG_TIN_NHAN();

            try
            {
                if (row["POThamChieuSAP"] != DBNull.Value)
                    objTHONG_TIN_NHAN.POThamChieuSAP = row["POThamChieuSAP"].ToString();
            }
            catch { }
            try
            {
                if (row["MaVTPT"] != DBNull.Value)
                    objTHONG_TIN_NHAN.MaVTPT = row["MaVTPT"].ToString();
            }
            catch { }
            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    objTHONG_TIN_NHAN.TenVTPT = (row["TenVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKho"] != DBNull.Value)
                    objTHONG_TIN_NHAN.MaKho = row["MaKho"].ToString();
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objTHONG_TIN_NHAN.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["SoLuongIn"] != DBNull.Value)
                    objTHONG_TIN_NHAN.SoLuongIn = int.Parse(row["SoLuongIn"].ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuIn"] != DBNull.Value)
                    objTHONG_TIN_NHAN.MaPhieuIn = int.Parse(row["MaPhieuIn"].ToString());
            }
            catch { }
            try
            {
                if (row["MaChiTietPDH"] != DBNull.Value)
                    objTHONG_TIN_NHAN.MaChiTietPDH = int.Parse(row["MaChiTietPDH"].ToString());
            }
            catch { }


            return objTHONG_TIN_NHAN;
        }
        private List<THONG_TIN_NHAN> GetTHONG_TIN_NHAN(DataTable dt)
        {
            List<THONG_TIN_NHAN> lst = new List<THONG_TIN_NHAN>();
            THONG_TIN_NHAN obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private Method

        #region Public Method
        public int usp_InsertPHIEU_IN_NHAN(THONG_TIN_NHAN obj)
        {
            int kq = -1;
            string spName = "usp_InsertPHIEU_IN_NHAN";           
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[10];
                par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                par[0].Direction = ParameterDirection.ReturnValue;
                par[1] = new SqlParameter("@MaPhieuIn", obj.MaPhieuIn);
                par[2] = new SqlParameter("@MaChiTietPDH", obj.MaChiTietPDH);
                par[3] = new SqlParameter("@POThamChieuSAP", obj.POThamChieuSAP);
                par[4] = new SqlParameter("@MaVTPT", obj.MaVTPT);
                par[5] = new SqlParameter("@SoLuongIn", obj.SoLuongIn);
                par[6] = new SqlParameter("@MaKho", obj.MaKho);
                par[7] = new SqlParameter("@MaTrangThai", obj.MaTrangThai);
                par[8] = new SqlParameter("@NguoiTao", obj.NguoiTao);
                par[9] = new SqlParameter("@NgayTao", obj.NgayTao);

                if (!SqlHelper.IsHaveTran())
                   SqlHelper.ExecuteNonQuery(spName, par);
                else
                {
                   SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                }

                kq = int.Parse(par[0].Value.ToString());
            }
            catch 
            {
                kq = -1;
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int usp_InsertPHIEU_IN_NHAN(List<THONG_TIN_NHAN> lst)
        {
            int kq = -1;
            string spName = "usp_InsertPHIEU_IN_NHAN";
            try
            {
                SqlHelper.BeginTran();
                SqlParameter[] par;
                THONG_TIN_NHAN obj;
                int i=0;
                for ( i = 0; i < lst.Count; i++)
                {
                    
                        obj = lst[i];
                        par = new SqlParameter[10];
                        par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                        par[0].Direction = ParameterDirection.ReturnValue;
                        par[1] = new SqlParameter("@MaPhieuIn", obj.MaPhieuIn);
                        par[2] = new SqlParameter("@MaChiTietPDH", obj.MaChiTietPDH);
                        par[3] = new SqlParameter("@POThamChieuSAP", obj.POThamChieuSAP);
                        par[4] = new SqlParameter("@MaVTPT", obj.MaVTPT);
                        par[5] = new SqlParameter("@SoLuongIn", obj.SoLuongIn);
                        par[6] = new SqlParameter("@MaKho", obj.MaKho);
                        par[7] = new SqlParameter("@MaTrangThai", obj.MaTrangThai);
                        par[8] = new SqlParameter("@NguoiTao", obj.NguoiTao);
                        par[9] = new SqlParameter("@NgayTao", obj.NgayTao);

                        SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                        kq = int.Parse(par[0].Value.ToString());

                        if (kq <= 0)
                            break;
                    
                }
                if (i == lst.Count)
                {
                    kq = 1;
                    SqlHelper.CommitTran();
                }
                else
                {
                    kq = -1;
                    SqlHelper.RollBackTran();
                }
                               
            }
            catch (Exception ex)
            {
                kq = -1;
            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }


        #endregion Public Method
    }
}
